package com.atguigu.service.impl;

import com.atguigu.dao.DictDao;
import com.atguigu.entity.Dict;
import com.atguigu.service.DictService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class DictServiceImpl implements DictService {

    @Autowired
    private DictDao dictDao;

    @Override
    public List<Map<String, Object>> findZondes(Long id) {
        //准备一个返回数据的集合
        //获取子节点数据列表
        List<Dict> dictList = dictDao.findListByParentId(id);

        //构建ztree数据
        List<Map<String,Object>> zNodes = new ArrayList<>();
        for(Dict dict : dictList) {
            Integer count = dictDao.countIsParent(dict.getId());
            Map<String,Object> map = new HashMap<>();
            map.put("id", dict.getId());
            map.put("isParent", count > 0 ? true : false);
            map.put("name", dict.getName());
            zNodes.add(map);
        };
        return zNodes;
    }

    /*
    * 为下拉列表中中查询北京中的城市根据id查询到对应的社区
    * */
    @Override
    public List<Dict> findListByParentId(Long parentId) {
        return dictDao.findListByParentId(parentId);
    }

    @Override
    public List<Dict> findListByDictCode(String dictCode) {
       Dict dict = dictDao.getByDictCode(dictCode);
       if(null == dict) return null;
        return this.findListByParentId(dict.getId());
    }

    @Override
    public String getNameById(Long houseId) {
      return  dictDao.getNameById(houseId);
    }

}

